/**
  ******************************************************************************
  * @file    PWR_CurrentConsumption/stm32f0xx_it.c 
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    23-March-2012
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_it.h"
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "IO_usart.h"
#include "rtc.h"
#include "NBIOT-BC95.h"

/** @addtogroup PWR_CurrentConsumption
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint16_t capture = 0;
//uint16_t led_tim = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M0 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
//  TimingDelay_Decrement();
}

/******************************************************************************/
/*                 STM32F0xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f0xx.s).                                               */
/******************************************************************************/
/**
  * @brief  This function handles EXTI Lines 0 to 1 interrupts requests.
  * @param  None
  * @retval None
  */
void EXTI0_1_IRQHandler(void)
{ 
//  if(EXTI_GetITStatus(USER_BUTTON_EXTI_LINE) != RESET)
//  {    
//    /* Clear the User Button EXTI line pending bit */
//    EXTI_ClearITPendingBit(USER_BUTTON_EXTI_LINE);
//  }
}

extern __IO uint8_t McuManualCheck ;
extern __IO uint8_t SensorAutoUpload ;

void EXTI2_3_IRQHandler(void)
{ 
  if(EXTI_GetITStatus(EXTI_Line3) != RESET)
  {    
    /* Clear the User Button EXTI line pending bit */
    EXTI_ClearITPendingBit(EXTI_Line3);
//    if(McuManualCheck)
//    {
//        SensorAutoUpload = 0 ;
//    }
//    else
//    {
   //     SensorAutoUpload = 1 ;
//    }
  }
}

/**
  * @brief  This function handles RTC Auto wake-up interrupt request.
  * @param  None
  * @retval None
  */
void RTC_IRQHandler(void)
{
//  RTC_TimeTypeDef   RTC_TimeStructure_now;
  if (RTC_GetITStatus(RTC_IT_ALRA) != RESET)
  {
    
//    RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure_now);
//    RTC_Set_AlarmA(RTC_TimeStructure_now.RTC_Hours,RTC_TimeStructure_now.RTC_Minutes,RTC_TimeStructure_now.RTC_Seconds+30);
//    SendStr("Reload123");
      
    /* Clear the Alarm A Pending Bit */
    RTC_ClearITPendingBit(RTC_IT_ALRA);
    
    /* Clear EXTI line17 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line17);    
  }
  
}

/**
  * @brief  This function handles TIM3 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM3_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
        
        LED_TimeHandler();
        Uart_TimeHandler();
        BC95_TimeHandler();
        TimingDelay_Decrement();
        capture = TIM_GetCapture3(TIM3);
        TIM_SetCompare3(TIM3, capture + CCR3_Val);
    }
    else
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);

        capture = TIM_GetCapture4(TIM3);
        TIM_SetCompare4(TIM3, capture + CCR4_Val);
    }
}
/**
* @brief  This function handles USART1 interrupt request.
* @param  None
* @retval None
*/
void USART1_IRQHandler(void)
{
  Uart_IrqHandler();
//  if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
//  { 
//    /* Clear The USART WU flag */  
//    
//    USART_ClearITPendingBit(USART1, USART_IT_RXNE);
////    InterruptCounter = 0x01;
//  }
}

void USART2_IRQHandler(void)
{
  Uart_IrqHandler();
////  if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
////  { 
////    /* Clear The USART WU flag */  
////    
////    USART_ClearITPendingBit(USART2, USART_IT_RXNE);
//////    InterruptCounter = 0x01;
////  }
}

/**
  * @brief  This function handles ADC1 global interrupts requests.
  * @param  None
  * @retval None
  */
void ADC1_COMP_IRQHandler(void)
{
  if(ADC_GetITStatus(ADC1, ADC_IT_AWD) != RESET)
  {
    /*  Turn on LED4 */
//    STM_EVAL_LEDOn(LED4);
//    LCD_SetTextColor(Red);

    /* Clear ADC1 AWD pending interrupt bit */
    ADC_ClearITPendingBit(ADC1, ADC_IT_AWD);


  }
}
/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
